<?php //$Id

/**
 * This file defines de main slides configuration form
 * It uses the standard core Moodle (>1.8) formslib. For
 * more info about them, please visit:
 * 
 * http://docs.moodle.org/en/Development:lib/formslib.php
 *
 * The form must provide support for, at least these fields:
 *   - name: text element of 64cc max
 *
 * Also, it's usual to use these fields:
 *   - intro: one htmlarea element to describe the activity
 *            (will be showed in the list of activities of
 *             slides type (index.php) and in the header 
 *             of the slides main page (view.php).
 *   - introformat: The format used to write the contents
 *             of the intro field. It automatically defaults 
 *             to HTML when the htmleditor is used and can be
 *             manually selected if the htmleditor is not used
 *             (standard formats are: MOODLE, HTML, PLAIN, MARKDOWN)
 *             See lib/weblib.php Constants and the format_text()
 *             function for more info
 */

require_once ('moodleform_mod.php');
include_once 'SSUtil/SSUtil.php';
include_once 'embood.php';
include_once 'emboodfetcher.php';

?>
<meta HTTP-EQUIV="Expires" content="27 Aug 2007 00:00:00 GMT" />
<meta HTTP-EQUIV="Cache-Control" content="no-cache" />
<LINK href="../mod/slides/slide_selector.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="../mod/slides/slide_form.js"></script>
<!-- Combo-handled YUI CSS files: -->
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/combo?2.7.0/build/logger/assets/skins/sam/logger.css">
<!-- Combo-handled YUI JS files: -->
<script type="text/javascript" src="http://yui.yahooapis.com/combo?2.7.0/build/yahoo/yahoo-debug.js&2.7.0/build/dom/dom-debug.js&2.7.0/build/event/event-debug.js&2.7.0/build/logger/logger-debug.js"></script>
<!-- Combo-handled YUI CSS files: -->
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/combo?2.7.0/build/button/assets/skins/sam/button.css&2.7.0/build/logger/assets/skins/sam/logger.css">
<!-- Combo-handled YUI JS files: -->
<script type="text/javascript" src="http://yui.yahooapis.com/combo?2.7.0/build/yahoo/yahoo-debug.js&2.7.0/build/dom/dom-debug.js&2.7.0/build/event/event-debug.js&2.7.0/build/element/element-debug.js&2.7.0/build/button/button-debug.js&2.7.0/build/logger/logger-debug.js"></script>



<?php
class mod_slides_mod_form extends moodleform_mod {
	
	function renderemboods($emboodList) {
		$mform   =& $this->_form;
  		foreach ($emboodList as $i) {   	
  			$thumbnailSource = $i->thumbnailurl;
		   	$id=$i->id;
		   	$description=$i->description;
		   	$title=$i->title;
		   	$embedcode = $i->embedcode;
		   	$hiddenid="embed".$i->id;
		   	
		   	//unique ids so slide_selector.js can find the desc and title, then populate the form fields
		   	$embedelement=&MoodleQuickForm::createElement('hidden','embed'.$id , $embedcode, "id=$hiddenid");
		   	$descriptionelement=&MoodleQuickForm::createElement('hidden','description'.$id , $description, "id=description".$id);
		   	$titleelement=&MoodleQuickForm::createElement('hidden','title'.$id , $title, "id=title".$id);
		   	//displayed title
		   	$label=$thumbnailSource."<span class='slidetitle'>".$title."</span>";
    		$slidechooser = &MoodleQuickForm::createElement('radio', 'slide_id','',$label,$id, "class=slide_selector onclick=slides_onclick($id)");
    		
    		$mform->insertElementBefore($embedelement,'placeholder');
    		$mform->insertElementBefore($descriptionelement,'placeholder');
    		$mform->insertElementBefore($titleelement,'placeholder');
    		$mform->insertElementBefore($slidechooser,'placeholder');
  		}
  	 	$mform->closeHeaderBefore('embedcode'); 
  	 	
      	$embedcode=&MoodleQuickForm::createElement('hidden','embedcode' , '', "id=embedcode");
      	$mform->insertElementBefore($embedcode,'placeholder');
	}

	function definition() {
		global $COURSE;
		$mform   =& $this->_form;
 		//----------
 		$mform->addElement('hidden','warningplaceholder');
 		
 		$emboodtypes =	array('slideshare' => 'SlideShare',
							'youtube' => 'YouTube');
 		$mform->addElement('select', 
							'emboodtype', 
							get_string('emboodtype', 'slides'),
							$emboodtypes);
		//rm->getElement('emboodtype')->setValue('slideshare');
 		$mform->registerNoSubmitButton('changeemboodtype');     
        $mform->addElement('submit', 'changeemboodtype', get_string('update'));
 		
 	
        
        //---------
        //add a placeholder. once we retrieve the slides for the user, we put them here
        $mform->addElement('hidden','placeholder');
 		
 		//------------------------------------------------------------------------------
 		// add standard buttons, common to all modules    
        $includeGroups=false;
		$this->standard_coursemodule_elements($includeGroups);
        $this->add_action_buttons();
        //$argh=$mform->getElement('emboodtype')->getValue();
        //echo 'dfine form'.$argh[0].'<br/>';
	}
	
	function definition_after_data() {
		//most of our stuff ends up here, because many of the form fields depend on what they
		//pick for the first few forms
		parent::definition_after_data();
    	
    	$mform =& $this->_form;
    
    	$newme=false; //have i changed who my slideshare user is?
    	
    	$emboodvalue=$mform->getElement('emboodtype')->getValue();
    	$emboodtype=$emboodvalue[0];
    	//********************************
    	//What type are we showing?
    	//********************************
    	if(empty($emboodtype)) {
    		$emboodtype='slideshare';
    	}
    	if($emboodtype=='slideshare') {
    		
    		$ssusername_pref=get_user_preferences('slides_ssusername');
	 		$ssusernamepreffield=&MoodleQuickForm::createElement('text','myssusername',get_string('slides_user','slides'),'value='.$ssusername_pref);
	 		$mform->insertElementBefore($ssusernamepreffield,'placeholder');
	 		$mform->registerNoSubmitButton('changemyssuser');  
	 		$changessusername=&$mform->createElement('submit', 'changemyssuser', get_string('update'));   
	        $mform->insertElementBefore($changessusername,'placeholder');
	        if(empty($ssusername_pref)) {	
 				$firsttimewarning = &MoodleQuickForm::createElement('static','firsttimewarning','',get_string('slides_nouserwarning','slides'));
 				$mform->insertElementBefore($firsttimewarning,'warningplaceholder');
 				return;
	        }
	        $myusernamevalue=$mform->getElement('myssusername')->getValue();
	        //echo 'myprefuser'.$myssuser.'<br/>';
	        //echo 'myuserfieldvalue'.$myusernamevalue.'<br/>';
	        if($ssusername_pref!=$myusernamevalue) {
	        	$newme=true;
	        }
	        
	        set_user_preferences(array('slides_ssusername' => $myusernamevalue));
	        //$myssuser=get_user_preferences('slides_ssusername');
	        //echo 'mysecondpreuser'.$myssuser.'<br/>';
    	}
    	elseif ($emboodtype=='youtube'){
    	}
   	
 		//*******************************
 		//Show options for slide filtering/navigation based on login
		//***********************************
 		//Create and add contacts
 		$myssuser=get_user_preferences('slides_ssusername');
 		$contacts =	array($myssuser => $myssuser.' (me)',
							'manastungare' => 'manastungare', 
							'jboutelle' => 'jboutelle',
							'this will be a list of your contacts!');
 		$contactselector=&MoodleQuickForm::createElement('select', 
														'contactselector', 
														get_string('slides_ssuser', 'slides'),
														$contacts);
		$mform->insertElementBefore($contactselector,'placeholder');
		if($newme) { //revert contact list to select myself 
			$mform->getElement('contactselector')->setValue($myssuser);
		}
		
		$selectedcontact=$mform->getElement('contactselector')->getValue();
 		if(!empty($selectedcontact)) {
 			$user_to_search_on=$selectedcontact[0];
 		}
 		else {
 			$user_to_search_on=$myssuser;
 		}
		$mform->registerNoSubmitButton('changeselectedcontact');     
        $changeselectedcontact=&$mform->createElement('submit', 'changeselectedcontact', get_string('update'));
        $mform->insertElementBefore($changeselectedcontact,'placeholder');

		
		
		//***********************************
		//Set up area to choose emboodle, fetch them, display them
		//***********************************
 		$slideheader=&MoodleQuickForm::createElement('header', 'abuttongroup', get_string('slideschooser', 'slides'));
  		$mform->insertElementBefore($slideheader,'placeholder');
  		
  		$myEmboodFetcher=new emboodFetcher;
  		if($emboodtype=='slideshare') {
  			$emboodlist=$myEmboodFetcher->fetchFromSS($user_to_search_on);
  			
  		}
  		elseif ($emboodtype=='youtube') {
  			$emboodlist=$myEmboodFetcher->fetchFromYT($user_to_search_on);
  		
  		}
  		
      	$this->renderemboods($emboodlist);
        
		//***********************************
    	// Adding the "general" fieldset, where all the common settings are showed
		//***********************************    	
        $generalheader=&MoodleQuickForm::createElement('header', 'general', get_string('general', 'form'));
        $mform->insertElementBefore($generalheader,'placeholder');
 		/// Adding the standard "name" field
        $namefield=&MoodleQuickForm::createElement('text', 'name', get_string('slides_title', 'slides'), array('size'=>'64'));
        $mform->insertElementBefore($namefield,'placeholder');
		$mform->setType('name', PARAM_TEXT);
		$mform->addRule('name', null, 'required', null, 'client');
    	/// Adding the optional "intro" and "introformat" pair of fields
    	$introfield=&$mform->createElement('textarea', 'intro', get_string('intro', 'slides'),'wrap="virtual" rows="10" cols="50"');
    	$mform->insertElementBefore($introfield,'placeholder');
		$mform->setType('intro', PARAM_RAW);
		$mform->addRule('intro', get_string('required'), 'required', null, 'client');
        $mform->setHelpButton('intro', array('writing', 'richtext'), false, 'editorhelpbutton');
	}
	
	
}

?>




